<!DOCTYPE html>
<style type="text/css">
    .tangram-suggestion-main {
        z-index: 99999999999;
    }
</style>
<html>
<head>
    <!-- 页面meta -->
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<meta name="viewport" content="initial-scale=1.0, user-scalable=no"/>
<!-- 引入样式 -->
<link rel="stylesheet" href="../plugins/elementui/index.css">
<link rel="stylesheet" href="../plugins/font-awesome/css/font-awesome.min.css">
<link rel="stylesheet" href="../css/style.css">
<link rel="stylesheet" href="https://cache.amap.com/lbs/static/main1119.css"/>

<style type="text/css">
    body, html {
        width: 100%;
        height: 100%;
        margin: 0;
        font-family: "微软雅黑";
        font-size: 14px;
    }

    #l-map {
        height: 200px;
        width: 400px;
    }

    #r-result {
        width: 100%;
    }
</style>
<script type="text/javascript" src="//api.map.baidu.com/api?v=2.0&ak=DWXt5d9kvrc80T3LVemDuiPawOHvNkLx"></script>
<style>
    .avatar-uploader .el-upload {
        border: 1px dashed #d9d9d9;
        border-radius: 6px;
        cursor: pointer;
        position: relative;
        overflow: hidden;
    }

    .avatar-uploader .el-upload:hover {
        border-color: #409EFF;
    }

    .avatar-uploader-icon {
        font-size: 28px;
        color: #8c939d;
        width: 178px;
        height: 178px;
        line-height: 178px;
        text-align: center;
    }

    .avatar {
        width: 178px;
        height: 178px;
        display: block;
    }

    .datatable {
        position: relative;
        box-sizing: border-box;
        -webkit-box-flex: 1;
        width: 100%;
        max-width: 100%;
        font-size: 14px;
        color: rgb(96, 98, 102);
        overflow: hidden;
        flex: 1 1 0%;
    }

    .datatable td, .datatable th {
        padding: 12px 0;
        min-width: 0;
        -webkit-box-sizing: border-box;
        box-sizing: border-box;
        text-overflow: ellipsis;
        vertical-align: middle;
        position: relative;
        text-align: left;
    }
</style>
</head>
<body class="hold-transition">
<div id="app">

    <div class="app-container">
        <div class="box">
            <div class="filter-container">
                <el-input placeholder="公司地址" v-model="pagination.queryString" style="width: 200px;"
                          class="filter-item"></el-input>
                <el-button @click="pagination.currentPage=1;findPage()" class="dalfBut">查询</el-button>
                <el-button type="primary" class="butT" @click="handleCreate()">新建</el-button>
            </div>
            <el-table size="small" current-row-key="id" :data="dataList" stripe highlight-current-row>
                <el-table-column prop="id" align="center" label="序号"></el-table-column>
                <el-table-column prop="address" align="center" label="公司地址"></el-table-column>
                <el-table-column prop="lng" align="center" label="所在经度"></el-table-column>
                <el-table-column prop="lat" align="center" label="所在纬度"></el-table-column>
                <el-table-column label="操作" align="center">
                    <template slot-scope="scope">
                        <el-button size="mini" type="danger" @click="handleDelete(scope.row)">删除</el-button>
                    </template>
                </el-table-column>
            </el-table>
            <div class="pagination-container">
                <el-pagination
                        class="pagiantion"
                        @current-change="handleCurrentChange"
                        :current-page="pagination.currentPage"
                        :page-size="pagination.pageSize"
                        layout="total, prev, pager, next, jumper"
                        :total="pagination.total">
                </el-pagination>
            </div>

            <!-- 新增标签弹层 -->
            <div class="add-form">
                <el-dialog title="新增机构地址" :visible.sync="dialogFormVisible">
                    <el-form ref="dataAddForm" :model="formData" :rules="rules" label-position="right"
                             label-width="100px">
                        <el-row>
                            <el-col :span="12">
                                <el-form-item label="详细地址" prop="address">
                                    <div id="r-result">
                                        <el-input id="suggestId" size="20" v-model="formData.address"
                                                  style="width:200px;"/>
                                    </div>
                                    <div id="l-map"></div>
                                    <div id="searchResultPanel"
                                         style="border:1px solid #C0C0C0;width:150px;height:auto; display:none;"></div>
                                </el-form-item>
                            </el-col>

                            <e-col>
                           &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
                                <el-button type="primary" round icon="el-icon-location" @click="location()">设置定位
                                </el-button>
                            </e-col>
                        </el-row>

                        <el-row>
                            <el-col :span="12" >
                                <el-form-item  >
                                   经纬度： <el-input type="text" id="lal" disabled />
                                </el-form-item>
                            </el-col>
                        </el-row>

                    </el-form>
                    <div slot="footer" class="dialog-footer">
                    <el-button @click="dialogFormVisible = false">取消</el-button>
                    <el-button type="primary" @click="handleAdd()">确定</el-button>
                </div>
                </el-dialog>
            </div>
        </div>
    </div>
</div>
</body>
<!-- 引入组件库 -->
<script src="../js/vue.js"></script>
<script src="../plugins/elementui/index.js"></script>
<script type="text/javascript" src="../js/jquery.min.js"></script>
<script src="../js/axios-0.18.0.js"></script>

<script type="text/javascript">
    var vue = new Vue({
        el: '#app',
        data: {
            pagination: {//分页相关模型数据
                currentPage: 1,//当前页码
                pageSize: 5,//每页显示的记录数
                total: 0,//总记录数
                queryString: null//查询条件
            },
            dataList: [],//当前页要展示的分页列表数据
            formData: {},//表单数据
            dialogFormVisible: false,//增加表单是否可见
            dialogFormVisible4Edit: false,//编辑表单是否可见
            rules: {//校验规则
                code: [{required: true, message: '项目编码为必填项', trigger: 'blur'}],
                name: [{required: true, message: '项目名称为必填项', trigger: 'blur'}]
            },
            address: "", //当前位置
        },
        //钩子函数，VUE对象初始化完成后自动执行
        created() {
            this.findPage();
        },
        methods: {
            //添加
            handleAdd() {
                this.$refs['dataAddForm'].validate((valid) => {
                    if (valid) {
                        this.formData.address = document.getElementById("suggestId").value;
                        this.formData.lal=document.getElementById("lal").value;
                        axios.post("/address/add.do", this.formData).then((res) => {
                            this.dialogFormVisible = false;
                            if (res.data.flag) {
                                //成功后调用查询页面
                                this.findPage();
                                //弹出提示
                                this.$message({
                                    message: res.data.message,
                                    type: 'success'
                                });
                            }
                            else {
                                this.$message.error(res.data.message);
                            }
                        })
                    }
                    else {
                        this.$message.error("数据校验失败，请检查你的输入信息是否正确!");
                    }
                })

            },
            //分页查询
            findPage() {
                var param = {
                    currentPage: this.pagination.currentPage,
                    pageSize: this.pagination.pageSize,
                    queryString: this.pagination.queryString
                };
                axios.post("/address/findPage.do", param).then((res) => {
                    this.dataList = res.data.rows;
                    this.pagination.total = res.data.total;
                })
            },
            // 重置表单
            resetForm() {
                this.formData = {};
                this.imageUrl = null;
            },

            // 弹出添加窗口
            handleCreate() {
                this.dialogFormVisible = true;
                this.resetForm();
                var script = document.createElement("script");
                script.type = "text/javascript";
                script.src = "../js/location.js";
                document.body.appendChild(script)
            },

            //定位
            location() {
                var map = new BMap.Map("container");
                var localSearch = new BMap.LocalSearch(map);
                var keyword = document.getElementById("suggestId").value;
                localSearch.setSearchCompleteCallback(function (searchResult) {
                    var poi = searchResult.getPoi(0);
                    document.getElementById("lal").value = poi.point.lng + "," + poi.point.lat;
                });
                localSearch.search(keyword);
            },

            //切换页码
            handleCurrentChange(currentPage) {
                this.pagination.currentPage = currentPage;
                this.findPage();
            },

            //权限不足
            showMessage(r) {
                if (r == 'Error: Request failed with status code 403') {
                    this.$message.error('无访问权限');
                    return;
                } else {
                    this.$message.error('未知错误');
                    return;
                }
            },
            // 删除
            handleDelete(row) {
                this.$confirm('你确定要删除当前数据吗', '提示', {
                    type: 'warning'
                }).then(() => {
                    axios.get("/address/delete.do?id=" + row.id).then((res) => {
                        if (res.data.flag) {
                            this.findPage();
                            //弹出成功信息
                            this.$message({
                                type: "success",
                                message: res.data.message
                            })
                        }
                        else {
                            this.$message.error(res.data.message);
                        }

                    }).catch((res) => {
                        this.showMessage(res);
                    })

                }).catch(() => {
                    this.$message({
                        type: "info",
                        message: "操作已取消"
                    })
                });
            }
        }
    })
</script>
</html>